package com.chain.BrotherExchange.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.LegalTenderDealList;
import com.chain.BrotherExchange.ui.activity.LegalTenderBuyOrSellActivity;
import com.chain.BrotherExchange.ui.activity.SettingTransactionPasswordActivity;
import com.chain.BrotherExchange.ui.activity.UserLegalTenderTradeInfoActivity;
import com.chain.BrotherExchange.ui.listener.LoginInterceptListener;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.ImageViews;
import com.chain.BrotherExchange.utils.ViewUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * 法币交易列表-购买/出售
 */
public class LegalTenderTransactionListAdapter extends RecyclerView.Adapter<LegalTenderTransactionListAdapter.ViewHolder> {

    private final int uid;
    private List<LegalTenderDealList.DataBean.ListBean> mLegalTenderDealList;

    public LegalTenderTransactionListAdapter(int uid) {
        this.uid = uid;
    }

    @NonNull
    @Override
    public LegalTenderTransactionListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_legal_tender_transaction_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LegalTenderTransactionListAdapter.ViewHolder holder, int i) {
        Resources resources = holder.itemView.getContext().getResources();
        LegalTenderDealList.DataBean.ListBean listBean = mLegalTenderDealList.get(i);
        holder.mTokenNameTextView.setText(listBean.getToken_name());
        holder.mUserNameTextView.setText(listBean.getUser_name());
        holder.mNumberTextView.setText(String.format(
                resources.getString(R.string.placeholder_number), listBean.getNumText()));
        holder.mUserVolumeTextView.setText(String.format(
                resources.getString(R.string.placeholder_have_been_trading), listBean.getUserVolumeText()));
        holder.mPriceTextView.setText(listBean.getPriceText());
        holder.mQuotaTextView.setText(String.format(resources.getString(R.string.placeholder_quota),
                listBean.getMinLimitText(), listBean.getMaxLimitText()));
        ImageViews.load(listBean.getUser_face(), holder.mUserAvatarImage, R.drawable.ic_placeholder_avatars);

        // 购买或出售
        if (listBean.getType_id() == 2) {
            holder.mBuyOrSellTextView.setText(R.string.buy);
        } else {
            holder.mBuyOrSellTextView.setText(R.string.sell);
        }

        // 购买或出售
        holder.mBuyOrSellTextView.setOnClickListener(new LoginInterceptListener(v -> {
            if (Androids.LocalConfig.isSetTransactionPassword()) {
                LegalTenderBuyOrSellActivity.launch(v.getContext(), listBean.getType_id(), listBean);
            } else {
                new AlertDialog.Builder(v.getContext()).setTitle(R.string.improve_information)
                        .setMessage(R.string.dialog_fund_password)
                        .setPositiveButton(R.string.go_setting, (dialog, which) ->
                                SettingTransactionPasswordActivity.launch(v.getContext())
                        ).setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()).show();
            }
        }));

        if (uid == 0) {
            // 查看用户交易信息
            holder.itemView.setOnClickListener(v ->
                    UserLegalTenderTradeInfoActivity.launch(v.getContext(), listBean.getUid()));
        } else {
            holder.mUserNameTextView.setText(listBean.getToken_name());
            holder.mUserAvatarImage.setImageResource(R.drawable.ic_token_image);
        }

        // 支付方式
        createPaymentView(listBean.getPays(), holder.mPaymentLayout);
    }

    private void createPaymentView(String pays, LinearLayout paymentLayout) {
        paymentLayout.removeAllViews();

        if (!TextUtils.isEmpty(pays)) {
            String[] payments = pays.split(",");
            HashSet<String> paymentSet = new HashSet<>(Arrays.asList(payments));

            for (String pay : paymentSet) {
                int resId = 0;
                switch (pay) {
                    case "1":
                        resId = R.drawable.ic_upcash_payment;
                        break;
                    case "3":
                        resId = R.drawable.ic_ali_payment;
                        break;
                    case "2":
                        resId = R.drawable.ic_we_chat;
                        break;
                    case "4":
                        resId = R.drawable.ic_pay_pal;
                        break;
                }

                Context context = paymentLayout.getContext();
                int size = (int) context.getResources().getDimension(R.dimen.dp_18);
                ImageView imageView = new ImageView(context);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(size, size));
                imageView.setImageResource(resId);
                ViewUtils.setMargins(imageView, 8,0,0,0);
                paymentLayout.addView(imageView);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mLegalTenderDealList != null ? mLegalTenderDealList.size() : 0;
    }

    public void setLegalTenderDealList(List<LegalTenderDealList.DataBean.ListBean> result) {
        mLegalTenderDealList = result;
        notifyDataSetChanged();
    }

    public void addLegalTenderDealList(List<LegalTenderDealList.DataBean.ListBean> source) {
        mLegalTenderDealList.addAll(source);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mQuotaTextView;
        private final TextView mPriceTextView;
        private final TextView mNumberTextView;
        private final TextView mUserNameTextView;
        private final ImageView mUserAvatarImage;
        private final TextView mUserVolumeTextView;
        private final TextView mTokenNameTextView;
        private final TextView mBuyOrSellTextView;
        private final LinearLayout mPaymentLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mBuyOrSellTextView = itemView.findViewById(R.id.buy_or_sale_view);
            mPriceTextView = itemView.findViewById(R.id.price_text);
            mUserNameTextView = itemView.findViewById(R.id.user_name_text);
            mNumberTextView = itemView.findViewById(R.id.number_text);
            mQuotaTextView = itemView.findViewById(R.id.quota_text);
            mUserAvatarImage = itemView.findViewById(R.id.user_avatar_image);
            mUserVolumeTextView = itemView.findViewById(R.id.have_been_trading_text);
            mTokenNameTextView = itemView.findViewById(R.id.token_name_text);
            mPaymentLayout = itemView.findViewById(R.id.payment_layout);
        }
    }
}
